package com.application.magazine;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jrummy.apps.dialogs.EasyDialog;
import com.jrummy.apps.dialogs.EasyDialog.ListItem;

import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class LeftMenuFragment extends Fragment {

	public static final int PHOTO_INDEX = 0;
	public static final int NOTICE_INDEX = 1;
	public static final int SET_INDEX = 2;

	private LeftMenuLayout layout = null;

	private int currentIndex = PHOTO_INDEX;
	private SparseArray<Fragment> rightFragments = new SparseArray<Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// return super.onCreateView(inflater, container, savedInstanceState);

		View view = inflater.inflate(R.layout.sliding_drawer_contents, null);

		layout = new LeftMenuLayout();
		layout.photo = (LinearLayout) view.findViewById(R.id.btn_drawer_photo);
//		layout.notice = (LinearLayout) view.findViewById(R.id.btn_drawer_notice);
//		layout.set = (LinearLayout) view.findViewById(R.id.btn_drawer_set);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		rightFragments.append(PHOTO_INDEX,((MainActivity) getActivity()).getPhotoFragment());
//		rightFragments.append(NOTICE_INDEX,
//				((MainActivity) getActivity()).getNoticeFragment());
//		rightFragments.append(SET_INDEX,
//				((MainActivity) getActivity()).getSetFragment());

		switchRightFragment(currentIndex);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		layout.photo.setOnClickListener(onClickListener);
//		layout.notice.setOnClickListener(onClickListener);
//		layout.set.setOnClickListener(onClickListener);
	}

	public void switchRightFragment(int position) {
		switch (position) {
		case PHOTO_INDEX:
			showPhotoFragment(true);
			break;
//		case NOTICE_INDEX:
//			showNoticeFragment(true);
//			break;
//		case SET_INDEX:
//			showSetFragment(true);
//			break;
		}

		drawButtonsBackground(position);
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_drawer_photo:
				showPhotoFragment(false);
				currentIndex = PHOTO_INDEX;
				String path = Environment.getExternalStorageDirectory().getAbsolutePath();
				List<ListItem> items = getFiles(path);

				EasyDialog dialog = new EasyDialog.Builder(getActivity())
				.setIcon(R.drawable.cate)
				.setTitle(getResources().getString(R.string.category_items))
				.setFastScrollEnabled(true)
				.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				})
				.setPositiveButton(R.string.db_close, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
				}).create();
				
				dialog.getListView().getLayoutParams().height = RelativeLayout.LayoutParams.MATCH_PARENT;

				dialog.show();
				break;
//			case R.id.btn_drawer_notice:
//				showNoticeFragment(false);
//				currentIndex = NOTICE_INDEX;
//				break;
//			case R.id.btn_drawer_set:
//				showSetFragment(false);
//				currentIndex = SET_INDEX;
//				break;
			}

			drawButtonsBackground(currentIndex);
		}
	};

	private boolean showPhotoFragment(boolean reset) {
		if (currentIndex == PHOTO_INDEX && !reset) {
			((MainActivity) getActivity()).getSlidingMenu().showContent();
			return true;
		}

		FragmentTransaction ft = getFragmentManager().beginTransaction();

		ft.hide(rightFragments.get(PHOTO_INDEX));
//		ft.hide(rightFragments.get(NOTICE_INDEX));
//		ft.hide(rightFragments.get(SET_INDEX));

		Fragment m = rightFragments.get(PHOTO_INDEX);

		((PhotoFragment) m).showMsg("Photo Fragment");

		ft.show(m);

		ft.commit();

		((MainActivity) getActivity()).getSlidingMenu().setTouchModeAbove(
				SlidingMenu.TOUCHMODE_FULLSCREEN);
		((MainActivity) getActivity()).getSlidingMenu().showContent();

		return false;
	}

	private boolean showNoticeFragment(boolean reset) {
		if (currentIndex == NOTICE_INDEX && !reset) {
			((MainActivity) getActivity()).getSlidingMenu().showContent();
			return true;
		}

		FragmentTransaction ft = getFragmentManager().beginTransaction();

		ft.hide(rightFragments.get(PHOTO_INDEX));
//		ft.hide(rightFragments.get(NOTICE_INDEX));
//		ft.hide(rightFragments.get(SET_INDEX));

		Fragment m = rightFragments.get(NOTICE_INDEX);

		((NoticeFragment) m).showMsg("Notice Fragment");

		ft.show(m);

		ft.commit();

		((MainActivity) getActivity()).getSlidingMenu().setTouchModeAbove(
				SlidingMenu.TOUCHMODE_FULLSCREEN);
		((MainActivity) getActivity()).getSlidingMenu().showContent();

		return false;
	}

	private boolean showSetFragment(boolean reset) {
		if (currentIndex == SET_INDEX && !reset) {
			((MainActivity) getActivity()).getSlidingMenu().showContent();
			return true;
		}

		FragmentTransaction ft = getFragmentManager().beginTransaction();

		ft.hide(rightFragments.get(PHOTO_INDEX));
//		ft.hide(rightFragments.get(NOTICE_INDEX));
//		ft.hide(rightFragments.get(SET_INDEX));

		Fragment m = rightFragments.get(SET_INDEX);

		((SetFragment) m).showMsg("Set Fragment");

		ft.show(m);

		ft.commit();

		((MainActivity) getActivity()).getSlidingMenu().setTouchModeAbove(
				SlidingMenu.TOUCHMODE_FULLSCREEN);
		((MainActivity) getActivity()).getSlidingMenu().showContent();

		return false;
	}

	private void drawButtonsBackground(int position) {
		layout.photo.setBackgroundResource(R.drawable.drawer_menu_btn);
//		layout.notice.setBackgroundResource(R.drawable.drawer_menu_btn);
//		layout.set.setBackgroundResource(R.drawable.drawer_menu_btn);

		switch (position) {
		case PHOTO_INDEX:
			layout.photo.setBackgroundResource(R.color.ics_blue_semi);
			break;
//		case NOTICE_INDEX:
//			layout.notice.setBackgroundResource(R.color.ics_blue_semi);
//			break;
//		case SET_INDEX:
//			layout.set.setBackgroundResource(R.color.ics_blue_semi);
//			break;
		}
	}

	private class LeftMenuLayout {
		LinearLayout photo;
//		LinearLayout notice;
//		LinearLayout set;
	}
	private List<ListItem> getFiles(String path) {
		List<ListItem> items = new ArrayList<ListItem>();
		Resources res = getResources();
		Drawable folderIcon = res.getDrawable(R.drawable.arrow_back);
		Drawable fileIcon = res.getDrawable(R.drawable.arrow_down);

		// Add the parent directory if this isn't the root directory
		if (!path.equals("/")) {
			ListItem item = new ListItem(folderIcon, "..", "Parent Folder");
			File parentFile = new File(path).getParentFile();
			String parent = parentFile == null ? "/" : parentFile.getPath();
			item.data = new File(parent);
			items.add(item);
		}

		File[] files = new File(path).listFiles();
		if (files == null) {
			return items;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("dd MMMMMMM, yyyy HH:mm:ss");

		for (File file : files) {
			String date = sdf.format(file.lastModified());
			Drawable icon = file.isDirectory() ? folderIcon : fileIcon;
			ListItem item = new ListItem(icon, file.getName(), date); // icon, label, sub-label
			item.data = file;
			items.add(item);
		}

		Collections.sort(items, EasyDialog.LIST_ITEM_COMPARATOR);

		return items;
	}
}
